﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading;

namespace BHair.Base
{
    public partial class frmjcxfNew : Form
    {
        public frmjcxfNew()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        string siweils = "";//四位流水号
        int yc = 0;
        string cardid = "";
        /// <summary>
        /// 查询卡内项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            if (label11.Text == "计次消费")
            {
                if (txtcardID.Text != string.Empty)
                {

                    cardid = txtcardID.Text;
                    

                    chuanzhi ca = new chuanzhi();
                    if (ca.checkpwdifnull(cardid) == false)//判断密码是否为空
                    {
                        //txtcardID.ReadOnly = true;
                        lsxf();


                        if (ca.CheckPass(cardid) == true)
                        {
                            if (ca.Checkyxq(cardid) == true)
                            {
                                Thread t1 = new Thread( new ThreadStart(kehuxinxi));
                                t1.Start();
                                //kehuxinxi();
                                Thread t2 = new Thread(new ThreadStart(ycxm));
                                t2.Start();
                                //ycxm();
                                SqlConnection conn = new SqlConnection(constr);
                                conn.Open();
                                string sql = string.Format("SELECT [jcspid] as 项目编号 ,jcxmname as 项目名称,[jcnum] as 项目次数 ,csmx.[备注]  FROM [csmx]  where csmx.cardID='{0}'", cardid);
                                DataTable dt = new DataTable();
                                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                                sda.Fill(dt);
                                dataGridView1.DataSource = dt;
                                conn.Close();
                            }
                            else
                            {
                                MessageBox.Show("此卡已过有效期！");
                                txtcardID.Text = null;
                            }
                        }
                        else
                        {
                            MessageBox.Show("卡号不存在！");
                            txtcardID.Text = null;
                        }

                    }
                    else
                    {
                        chuanzhi.cardidpp = cardid;
                        frmcheckpwd pwd = new frmcheckpwd();
                        pwd.ShowDialog();
                        if (chuanzhi.ifpwd == 1)
                        {
                            //txtcardID.ReadOnly = true;
                            lsxf();


                            if (ca.CheckPass(cardid) == true)
                            {
                                if (ca.Checkyxq(cardid) == true)
                                {
                                    Thread t7 = new Thread(new ThreadStart(kehuxinxi));
                                    // kehuxinxi();
                                    t7.Start();
                                    Thread t8 = new Thread(new ThreadStart(ycxm));
                                    t8.Start();
                                    //ycxm();
                                    SqlConnection conn = new SqlConnection(constr);
                                    conn.Open();
                                    string sql = string.Format("SELECT [jcspid] as 项目编号 ,jcxmname as 项目名称,[jcnum] as 项目次数 FROM [csmx]  where csmx.cardID='{0}'", cardid);
                                    DataTable dt = new DataTable();
                                    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                                    sda.Fill(dt);
                                    dataGridView1.DataSource = dt;
                                    conn.Close();
                                }
                                else
                                {
                                    MessageBox.Show("此卡已过有效期！");
                                    txtcardID.Text = null;
                                }
                            }
                            else
                            {
                                MessageBox.Show("卡号不存在！");
                                txtcardID.Text = null;
                            }
                        }
                        else
                        {
                            txtcardID.Text = null;
                        }
                    }

                }
                else
                {
                    MessageBox.Show("请输入卡号！");
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = string.Format("SELECT xmid, xmname, xfcs,csbz,cardid as 卡号  FROM jcxf WHERE (djbh = '{0}')",txtcardID.Text);
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                //dataGridView2.AutoGenerateColumns = false;
                //autogeneratecolumns = false;
                dataGridView2.DataSource = dt;
                conn.Close();
            }
          
            }
        
        /// <summary>
        /// 选择项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
                lbbianhap.Text = dataGridView1.SelectedCells[0].Value.ToString();
                txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
                yc = Int32.Parse(dataGridView1.SelectedCells[2].Value.ToString());
                lbbz.Text= dataGridView1.SelectedCells[3].Value.ToString();
            }
            catch
            {
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //检查卡内是否有此项目
            string cardid = txtcardID.Text;//卡号
            string xmid = lbbianhap.Text;
            chuanzhi cz = new chuanzhi();
            if (cz.checkxm(cardid, xmid) == true)
            {
                if (txtcishu.Text != string.Empty && txtName.Text != string.Empty)
                {
                    int cishu = Int32.Parse(txtcishu.Text.Trim());
                    if (yc < cishu)
                    {
                        MessageBox.Show("次数不够请充值");
                    }
                    else
                    {
                        int index = this.dataGridView2.Rows.Add();
                        this.dataGridView2.Rows[index].Cells[0].Value = lbbianhap.Text;
                        this.dataGridView2.Rows[index].Cells[1].Value = txtName.Text;
                        this.dataGridView2.Rows[index].Cells[2].Value = txtcishu.Text;
                        this.dataGridView2.Rows[index].Cells[3].Value = lbbz.Text;
                    }
                }
                else
                {
                    MessageBox.Show("请输入次数,或正确选择项目");
                }
            }
            else
            {
                MessageBox.Show("卡内无此项目，请重新输入卡号并按回车键确定！");
                txtcardID.Text = "";
                shuaxin();
            }
            }
        /// <summary>
        /// 扣减次数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (label11.Text == "计次消费")
            {
                if (txtcardID.Text != string.Empty & dataGridView2.Rows.Count > 0)
                {
                    SqlConnection conn = new SqlConnection(constr);
                    try
                    {
                        conn.Open();
                        int row = 0;
                        if (cardid != string.Empty)
                        {
                            for (int i = 0; i < dataGridView2.Rows.Count; i++)
                            {
                                //查询剩余次数
                                SqlConnection connyc = new SqlConnection(constr);
                                connyc.Open();
                                string sqlyc =string.Format( "SELECT [jcnum] FROM [csmx] where cardID='{0}' and jcspid='{1}'  and 备注='{2}'",txtcardID.Text, dataGridView2.Rows[i].Cells[0].Value.ToString(), dataGridView2.Rows[i].Cells[3].Value.ToString());
                                int sycs=0;
                                SqlCommand commyc = new SqlCommand(sqlyc,connyc);
                                SqlDataReader sdr = commyc.ExecuteReader();
                                while (sdr.Read())
                                {
                                    sycs =Int32.Parse( sdr["jcnum"].ToString());
                                }
                                //kouci
                                connyc.Close();
                                int upcs = sycs - Int32.Parse(this.dataGridView2.Rows[i].Cells[2].Value.ToString());
                                string sql = string.Format("UPDATE [csmx] SET [jcnum] ={0} WHERE csmx.cardID='{1}' and csmx.jcspid='{2}'and 备注='{3}'", upcs, cardid, this.dataGridView2.Rows[i].Cells[0].Value.ToString(), this.dataGridView2.Rows[i].Cells[3].Value.ToString());
                                SqlCommand comm = new SqlCommand(sql, conn);
                                row = comm.ExecuteNonQuery();
                                if (row > 0)
                                {
                                    string sql1 = string.Format("INSERT INTO [jcxf] ([djbh] ,[xmid] ,[xmname] ,[xfcs],[riqi] ,[cardid] ,[bz],[班组],[bs],[bz1],csbz,card)  VALUES('{0}','{1}' ,'{2}' ,'{3}' ,'{4}','{5}' ,'{6}','{7}',0,'{8}','{9}','{10}')", txtdjh.Text, this.dataGridView2.Rows[i].Cells[0].Value.ToString(), this.dataGridView2.Rows[i].Cells[1].Value.ToString(), this.dataGridView2.Rows[i].Cells[2].Value.ToString(), DateTime.Now, cardid, txtbz.Text, chuanzhi.banzu, txtbz1.Text, this.dataGridView2.Rows[i].Cells[3].Value.ToString(),label13.Text);
                                    SqlConnection conn1 = new SqlConnection(constr);
                                    conn1.Open();
                                    SqlCommand comm1 = new SqlCommand(sql1, conn1);
                                    comm1.ExecuteNonQuery();
                                }

                            }
                        }
                        else
                        {
                            MessageBox.Show("卡号为空请重新消费");
                        }
                        djhshuaxin();
                        djh();
                        upmasttime();
                        shuaxin();
                        if (row > 0)
                        {
                            MessageBox.Show("结算成功");
                            txtcardID.ReadOnly = false;
                        }
                        else { MessageBox.Show("没有扣次数没有扣次数！！！！！！！！！！！！"); }
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请正确录入单据！");
                }
            }
            else
            {
                //退费处理
                //根据卡号和项目ID查询项目次数，
                SqlConnection conn = new SqlConnection(constr);
                SqlConnection conn1 = new SqlConnection(constr);
                SqlConnection conn2 = new SqlConnection(constr);
                SqlConnection conn3 = new SqlConnection(constr);
                SqlConnection conn4 = new SqlConnection(constr);
                conn.Open();
                conn1.Open();
                conn2.Open();
                conn3.Open();
                conn4.Open();
                string cs = "";
               
                for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    string sql = string.Format("SELECT jcnum FROM [csmx] where cardID='{0}' and jcspid='{1}' and 备注='{2}'", this.dataGridView2.Rows[i].Cells[4].Value.ToString(), this.dataGridView2.Rows[i].Cells[0].Value.ToString(), this.dataGridView2.Rows[i].Cells[3].Value);
                    //textBox1.Text = sql;
                    
                    SqlCommand comm = new SqlCommand(sql,conn);
                    SqlDataReader sdr = comm.ExecuteReader();
                    while(sdr.Read())
                    {
                        cs =sdr["jcnum"].ToString();

                        //退回次数加上剩余次数修改
                        
                       int upcs =Int32.Parse( cs) + Int32.Parse(this.dataGridView2.Rows[i].Cells[2].Value.ToString());
                        //MessageBox.Show(upcs.ToString());
                        string sql1 =string.Format( "UPDATE csmx SET jcnum ={0} WHERE (cardID = '{1}') AND (jcspid = '{2}') and 备注='{3}'",upcs, this.dataGridView2.Rows[i].Cells[4].Value.ToString(), this.dataGridView2.Rows[i].Cells[0].Value.ToString(), this.dataGridView2.Rows[i].Cells[3].Value.ToString());
                        SqlCommand comm1 = new SqlCommand(sql1,conn1);
                        comm1.ExecuteNonQuery();
                        string sql2 = string.Format("UPDATE jcxf SET bs =2 where djbh='{0}'", txtcardID.Text);
                        SqlCommand comm2 = new SqlCommand(sql2, conn2);

                        comm2.ExecuteNonQuery();
                        string sql3 = string.Format("INSERT INTO [tuikuan]([djh], [xmid], [xmname], [cishu], [time]) VALUES('{0}', '{1}', '{2}',{3}, '{4}')",txtcardID.Text, this.dataGridView2.Rows[i].Cells[0].Value.ToString(), this.dataGridView2.Rows[i].Cells[1].Value.ToString(), this.dataGridView2.Rows[i].Cells[2].Value.ToString(),DateTime.Now.ToString());
                        SqlCommand comm3 = new SqlCommand(sql3,conn3);
                        comm3.ExecuteNonQuery();
                        string sql4 =string.Format( "INSERT INTO [jcxf]( [djbh], [xmid], [xmname], [xfcs], [riqi], [cardid], [bz], [班组],bs) VALUES('{0}', {1}, '{2}',{3}, '{4}','{5}','{6}','{7}',1)",txtcardID.Text, this.dataGridView2.Rows[i].Cells[0].Value.ToString(), this.dataGridView2.Rows[i].Cells[1].Value.ToString(), this.dataGridView2.Rows[i].Cells[2].Value.ToString(), DateTime.Now, this.dataGridView2.Rows[i].Cells[4].Value.ToString(), "", this.dataGridView2.Rows[i].Cells[4].Value.ToString(), chuanzhi.banzu);
                        SqlCommand comm4 = new SqlCommand(sql4,conn4);
                        comm4.ExecuteNonQuery();

                        chuanzhi cz1 = new chuanzhi();
                        cz1.rizhi("卡号："+ this.dataGridView2.Rows[i].Cells[3].Value.ToString() + "的项目"+ this.dataGridView2.Rows[i].Cells[1].Value.ToString()+"退次"+ this.dataGridView2.Rows[i].Cells[2].Value.ToString()+"次");
                    }
                    //删除单据
                    //conn.Close();

                    sdr.Close();
                }
                /*
                string sql2 = string.Format("DELETE FROM jcxf where djbh='{0}'",txtcardID.Text);
                SqlCommand comm2 = new SqlCommand(sql2,conn2);
                
                comm2.ExecuteNonQuery();
                */
                MessageBox.Show("退回成功");
                //this.shuaxin();
                //InitializeComponent();
                chuanzhi cz = new chuanzhi();
                cz.rizhi("计次退费单据号："+txtcardID.Text);
                conn.Close();
                conn1.Close();
                conn2.Close();
                conn3.Close();
                conn4.Close();
                dataGridView2.DataSource = null;
                DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                acCode.Name = "Column1";
                acCode.DataPropertyName = "xmid";
                acCode.HeaderText = "项目编号";
                dataGridView2.Columns.Add(acCode);
                DataGridViewTextBoxColumn j1 = new DataGridViewTextBoxColumn();
                j1.Name = "Column2";
                j1.DataPropertyName = "xmname";
                j1.HeaderText = "项目名称";
                dataGridView2.Columns.Add(j1);
                DataGridViewTextBoxColumn j2 = new DataGridViewTextBoxColumn();
                j2.Name = "Column3";
                j2.DataPropertyName = "xfcs";
                j2.HeaderText = "消费次数";
                dataGridView2.Columns.Add(j2);
                label11.Text = "计次消费";
                button4.Text = "退次";
                button3.Text = "保存";
                label1.Text = "会员卡：";
                txtcardID.Text = "";
                this.txtcardID.Focus();
                //InitializeComponent();
                //this.Refresh();
                //this.Close();
            }
        }
        public void shuaxin()
        {
            //txtcardID.Text = "";
            txtcishu.Text = "";
            txtName.Text = "";
            txtbz.Text = "";
            txtcishu.Text = "1";
            txtbz1.Text = "";
            dataGridView1.DataSource = null;
            DataGridView dgv2 = dataGridView2;
            dgv2.Rows.Clear();
            this.txtcardID.Focus();
        }
        public void kehuxinxi()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT mname, mphone,card.cname,积分 FROM member join card on member.mcid=card.cid where mid='{0}' or mphone='{1}'", txtcardID.Text, txtcardID.Text);
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                lbname.Text = sdr["mname"].ToString();
                lbphone.Text = sdr["mphone"].ToString();
                label13.Text = sdr["cname"].ToString();
                label15.Text = sdr["积分"].ToString();
            }
            conn.Close();
        }

        private void frmjcxfNew_Load(object sender, EventArgs e)
        {
            djh();
        }
        /// <summary>
        /// 生成单据号
        /// </summary>
        public void djh()
        {
            string lsh = "";
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = "SELECT [jcxf] FROM [djh]";
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                while (sdr.Read())
                {
                    lsh = sdr["jcxf"].ToString();
                }
                string bs = "JC-";
                string time = DateTime.Now.ToString("yyyy-MM-dd");
                /*
                Random ran = new Random();
                int RandKey = ran.Next(10, 999);
                */
                int djlsh = Int32.Parse(lsh) + 1;
                siweils = string.Format("{0:00000}", djlsh);
                string djh = bs + time + "-" +chuanzhi.banzu+ siweils;
                this.txtdjh.Text = djh;
                conn.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
        /// <summary>
        /// 刷新单据号
        /// </summary>
        public void djhshuaxin()
        {
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();

                string sql = string.Format("UPDATE [djh] SET [jcxf] = '{0}'", siweils);
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
            }
            catch { }
        }
        /// <summary>
        /// 修改最后光临时间
        /// </summary>
        public void upmasttime()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("UPDATE [member] SET  [mlastime] ='{0}' WHERE mid='{1}'", DateTime.Now.ToString(),txtcardID.Text);
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }
        /// <summary>
        /// 插入计次消费表
        /// </summary>
        public void insertjcxf()
        {
            //string sql = "INSERT INTO [BHairDB].[dbo].[jcxf] ([djbh] ,[xmid] ,[xmname] ,[xfcs],[riqi] ,[cardid] ,[bz])  VALUES('{0}',{1} ,'{2}' ,{3} ,'{4}','{5}' ,'{6}')";
        }
        public DataSet lsxf()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format("SELECT   [xmname] as 名称 ,[xfcs] as 次数,[riqi] as 日期 ,[bz] as 体重 ,bz1 as 备注,[djbh] as 单据号 FROM [jcxf] where cardid='{0}' and bs=0 order by id desc", cardid);
            
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView3.DataSource = dt;
            conn.Close();
            return ds;
        }

        private void txtcardID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (label11.Text == "计次消费")
                {
                    if (txtcardID.Text != string.Empty)
                    {

                        cardid = txtcardID.Text;


                        chuanzhi ca = new chuanzhi();
                        if (ca.checkpwdifnull(cardid) == false)//判断密码是否为空
                        {
                            //txtcardID.ReadOnly = true;
                            lsxf();


                            if (ca.CheckPass(cardid) == true)
                            {
                                if (ca.Checkyxq(cardid) == true)
                                {
                                    Thread t1 = new Thread(new ThreadStart(kehuxinxi));
                                    t1.Start();
                                    //kehuxinxi();
                                    Thread t2 = new Thread(new ThreadStart(ycxm));
                                    t2.Start();
                                    //ycxm();
                                    SqlConnection conn = new SqlConnection(constr);
                                    conn.Open();
                                    string sql = string.Format("SELECT [jcspid] as 项目编号 ,jcxmname as 项目名称,[jcnum] as 项目次数 ,csmx.[备注]  FROM [csmx]  where csmx.cardID='{0}'", cardid);
                                    DataTable dt = new DataTable();
                                    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                                    sda.Fill(dt);
                                    dataGridView1.DataSource = dt;
                                    conn.Close();
                                }
                                else
                                {
                                    MessageBox.Show("此卡已过有效期！");
                                    txtcardID.Text = null;
                                }
                            }
                            else
                            {
                                MessageBox.Show("卡号不存在！");
                                txtcardID.Text = null;
                            }

                        }
                        else
                        {
                            chuanzhi.cardidpp = cardid;
                            frmcheckpwd pwd = new frmcheckpwd();
                            pwd.ShowDialog();
                            if (chuanzhi.ifpwd == 1)
                            {
                                //txtcardID.ReadOnly = true;
                                lsxf();


                                if (ca.CheckPass(cardid) == true)
                                {
                                    if (ca.Checkyxq(cardid) == true)
                                    {
                                        Thread t7 = new Thread(new ThreadStart(kehuxinxi));
                                        // kehuxinxi();
                                        t7.Start();
                                        Thread t8 = new Thread(new ThreadStart(ycxm));
                                        t8.Start();
                                        //ycxm();
                                        SqlConnection conn = new SqlConnection(constr);
                                        conn.Open();
                                        string sql = string.Format("SELECT [jcspid] as 项目编号 ,jcxmname as 项目名称,[jcnum] as 项目次数 FROM [csmx]  where csmx.cardID='{0}'", cardid);
                                        DataTable dt = new DataTable();
                                        SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                                        sda.Fill(dt);
                                        dataGridView1.DataSource = dt;
                                        conn.Close();
                                    }
                                    else
                                    {
                                        MessageBox.Show("此卡已过有效期！");
                                        txtcardID.Text = null;
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("卡号不存在！");
                                    txtcardID.Text = null;
                                }
                            }
                            else
                            {
                                txtcardID.Text = null;
                            }
                        }

                    }
                    else
                    {
                        MessageBox.Show("请输入卡号！");
                    }
                }
                else
                {
                    SqlConnection conn = new SqlConnection(constr);
                    conn.Open();
                    string sql = string.Format("SELECT xmid, xmname, xfcs,csbz,cardid as 卡号 FROM jcxf WHERE (djbh = '{0}')", txtcardID.Text);
                    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    //dataGridView2.AutoGenerateColumns = false;
                    //autogeneratecolumns = false;
                    dataGridView2.DataSource = dt;
                    conn.Close();
                }


            }
        }
        public void ycxm()
        {
            try
            {
                Control.CheckForIllegalCrossThreadCalls = false;
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = string.Format("SELECT [jcspid] as 项目编号 ,items.iname as 预存项目,[jcnum] as 预存次数,date1 as 预存时间 FROM [ycxm] join items on ycxm.jcspid=items.iid where ycxm.cardID='{0}'", cardid);
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView4.DataSource = dt;
                conn.Close();
            }
            catch { }
        }

        private void dataGridView4_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {

            if (e.RowIndex >= dataGridView4.Rows.Count - 1)
                return;
            DataGridViewRow dgr = dataGridView4.Rows[e.RowIndex];
            try
            {
                //dgr.Cells[0]是当前性别列的索引值，用以确定判断哪一列的值
                
                    //定义画笔，使用颜色是深灰。
                    using (SolidBrush brush = new SolidBrush(Color.Red))
                    {
                        //利用画笔填充当前行
                        e.Graphics.FillRectangle(brush, e.RowBounds);
                        //将值重新写回当前行。
                        e.PaintCellsContent(e.ClipBounds);
                        e.Handled = true;
                    }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in dataGridView2.SelectedRows)
            {
                if (dr.IsNewRow == false)//如果不是已提交的行，默认情况下在添加一行数据成功后，DataGridView为新建一行作为新数据的插入位置
                    dataGridView2.Rows.Remove(dr);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (label11.Text == "计次消费")
            {
                label11.Text = "计次退费";
                button4.Text = "销售";
                label1.Text = "单据号：";
                button3.Text = "退回";
                
            }
            else if(label11.Text== "计次退费")
            {
                label11.Text = "计次消费";
                button4.Text = "退次";
                button3.Text = "保存";
                label1.Text = "会员卡：";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            chuanzhi.czcard = txtcardID.Text;
            frmczselect cs = new frmczselect();
            cs.ShowDialog();
        }

        private void txtcardID_Leave(object sender, EventArgs e)
        {
            if (label11.Text == "计次消费")
            {
                if (txtcardID.Text != string.Empty)
                {

                    cardid = txtcardID.Text;


                    chuanzhi ca = new chuanzhi();
                    if (ca.checkpwdifnull(cardid) == false)//判断密码是否为空
                    {
                        //txtcardID.ReadOnly = true;
                        lsxf();


                        if (ca.CheckPass(cardid) == true)
                        {
                            if (ca.Checkyxq(cardid) == true)
                            {
                                Thread t1 = new Thread(new ThreadStart(kehuxinxi));
                                t1.Start();
                                //kehuxinxi();
                                Thread t2 = new Thread(new ThreadStart(ycxm));
                                t2.Start();
                                //ycxm();
                                SqlConnection conn = new SqlConnection(constr);
                                conn.Open();
                                string sql = string.Format("SELECT [jcspid] as 项目编号 ,jcxmname as 项目名称,[jcnum] as 项目次数 ,csmx.[备注]  FROM [csmx]  where csmx.cardID='{0}'", cardid);
                                DataTable dt = new DataTable();
                                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                                sda.Fill(dt);
                                dataGridView1.DataSource = dt;
                                conn.Close();
                            }
                            else
                            {
                                MessageBox.Show("此卡已过有效期！");
                                txtcardID.Text = null;
                            }
                        }
                        else
                        {
                            MessageBox.Show("卡号不存在！");
                            txtcardID.Text = null;
                        }

                    }
                    else
                    {
                        chuanzhi.cardidpp = cardid;
                        frmcheckpwd pwd = new frmcheckpwd();
                        pwd.ShowDialog();
                        if (chuanzhi.ifpwd == 1)
                        {
                            //txtcardID.ReadOnly = true;
                            lsxf();


                            if (ca.CheckPass(cardid) == true)
                            {
                                if (ca.Checkyxq(cardid) == true)
                                {
                                    Thread t7 = new Thread(new ThreadStart(kehuxinxi));
                                    // kehuxinxi();
                                    t7.Start();
                                    Thread t8 = new Thread(new ThreadStart(ycxm));
                                    t8.Start();
                                    //ycxm();
                                    SqlConnection conn = new SqlConnection(constr);
                                    conn.Open();
                                    string sql = string.Format("SELECT [jcspid] as 项目编号 ,jcxmname as 项目名称,[jcnum] as 项目次数 FROM [csmx]  where csmx.cardID='{0}'", cardid);
                                    DataTable dt = new DataTable();
                                    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                                    sda.Fill(dt);
                                    dataGridView1.DataSource = dt;
                                    conn.Close();
                                }
                                else
                                {
                                    MessageBox.Show("此卡已过有效期！");
                                    txtcardID.Text = null;
                                }
                            }
                            else
                            {
                                MessageBox.Show("卡号不存在！");
                                txtcardID.Text = null;
                            }
                        }
                        else
                        {
                            txtcardID.Text = null;
                        }
                    }

                }
                else
                {
                    MessageBox.Show("请输入卡号！");
                }
            }
            else
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = string.Format("SELECT xmid, xmname, xfcs,csbz,cardid as 卡号 FROM jcxf WHERE (djbh = '{0}')", txtcardID.Text);
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                //dataGridView2.AutoGenerateColumns = false;
                //autogeneratecolumns = false;
                dataGridView2.DataSource = dt;
                conn.Close();
            }

        }

        private void dataGridView3_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
           
        }

        private void dataGridView3_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
       e.RowBounds.Location.Y,
       dataGridView1.RowHeadersWidth - 4,
       e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
    }

